package com.binhthuan.catechist.client.content.training;

import com.binhthuan.catechist.client.AbstractContent;
import com.binhthuan.catechist.client.ContentFactory;
import com.binhthuan.catechist.client.ECApplication;
import com.binhthuan.catechist.client.ECIcons;
import com.binhthuan.catechist.client.ECUser;
import com.binhthuan.catechist.client.data.CourseDS;
import com.binhthuan.catechist.client.data.TrainerDS;
import com.binhthuan.catechist.client.data.YearDS;
import com.binhthuan.catechist.client.grid.AbstractGrid;
import com.binhthuan.catechist.client.grid.CourseTrainersGrid;
import com.binhthuan.catechist.client.grid.TrainerAssignmentGrid;
import com.binhthuan.catechist.client.grid.TrainerGrid;
import com.binhthuan.catechist.shared.Utilities;
import com.binhthuan.catechist.shared.constant.Action;
import com.binhthuan.catechist.shared.constant.Parameter;
import com.smartgwt.client.data.Criteria;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.SelectionStyle;
import com.smartgwt.client.types.VisibilityMode;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.Label;
import com.smartgwt.client.widgets.events.DrawEvent;
import com.smartgwt.client.widgets.events.DrawHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.SelectItem;
import com.smartgwt.client.widgets.form.fields.events.ChangedEvent;
import com.smartgwt.client.widgets.form.fields.events.ChangedHandler;
import com.smartgwt.client.widgets.grid.events.DataArrivedEvent;
import com.smartgwt.client.widgets.grid.events.DataArrivedHandler;
import com.smartgwt.client.widgets.grid.events.RecordClickEvent;
import com.smartgwt.client.widgets.grid.events.RecordClickHandler;
import com.smartgwt.client.widgets.grid.events.SelectionChangedHandler;
import com.smartgwt.client.widgets.grid.events.SelectionEvent;
import com.smartgwt.client.widgets.layout.SectionStack;
import com.smartgwt.client.widgets.layout.SectionStackSection;
import com.smartgwt.client.widgets.layout.VLayout;

public class TrainerAssignmentPage extends AbstractContent {

    public static class Factory implements ContentFactory {

        private static Factory instance;
        private String id;

        private Factory() {
            if (id == null) {
                id = Utilities.generateID(TrainerAssignmentPage.class.getName());
            }
        }

        /**
         * @return the instance
         */
        public static Factory getInstance() {
            if (instance == null) {
                instance = new Factory();
            }

            return instance;
        }

        @Override
        public boolean checkCertificated(long securityCode) {
            return true;
        }

        @Override
        public AbstractContent create() {
            TrainerAssignmentPage content = new TrainerAssignmentPage();
            return content;
        }

        @Override
        public String getID() {
            return id;
        }

        @Override
        public String getIcon() {
            return ECIcons.getIcon("book.png");
        }

        @Override
        public String getTitle() {
            return "Trainer assignment";
        }
    }

    private AbstractGrid reportGrid;
    private AbstractGrid detailGrid;
    private Label totalItems;

    @Override
    public Canvas getContentPanel() {
        VLayout mainLayout = new VLayout();
        mainLayout.setWidth100();
        mainLayout.setHeight100();

        SectionStack stack = new SectionStack();
        stack.setWidth100();
        stack.setHeight100();
        stack.setAnimateSections(true);
        stack.setShowEdges(true);
        stack.setVisibilityMode(VisibilityMode.MULTIPLE);

        totalItems = new Label();
        totalItems.setAlign(Alignment.RIGHT);

        detailGrid = new CourseTrainersGrid(TrainerDS.getInstance());
        detailGrid.setAutoFetchData(false);
        detailGrid.setCanRemoveRecords(true);

        reportGrid = new TrainerAssignmentGrid(CourseDS.getInstance());
        reportGrid.setSelectionType(SelectionStyle.SINGLE);
        reportGrid.addDataArrivedHandler(new DataArrivedHandler() {

            @Override
            public void onDataArrived(DataArrivedEvent event) {
                totalItems.setContents(ECApplication.getMessages().numberOfResult(reportGrid.getTotalRows()));
                reportGrid.deselectAllRecords();
            }
        });

        reportGrid.addSelectionChangedHandler(new SelectionChangedHandler() {
            
            @Override
            public void onSelectionChanged(SelectionEvent event) {
                if(event.getState()) {
                    Criteria crit = new Criteria(Parameter.OPERATION_INFO, Action.GET_TRAINERS_BY_COURSE);
                    crit.addCriteria(Parameter.COURSE, event.getSelectedRecord().getAttribute(Parameter.ID));
                    detailGrid.fetchData(crit);
                }
            }
        });

        final SelectItem yearSelection = new SelectItem(Parameter.YEAR);
        yearSelection.setTitle(ECApplication.getConstants().schoolYear());
        yearSelection.setWidth(90);
        yearSelection.setDefaultToFirstOption(true);
        yearSelection.setOptionDataSource(YearDS.getInstance());
        yearSelection.setValueField(Parameter.ID);
        yearSelection.setDisplayField(Parameter.DESCRIPTION);
        yearSelection.setWrapTitle(false);
        yearSelection.addDataArrivedHandler(new com.smartgwt.client.widgets.form.fields.events.DataArrivedHandler() {

            @Override
            public void onDataArrived(com.smartgwt.client.widgets.form.fields.events.DataArrivedEvent event) {
                Criteria crit = new Criteria(Parameter.OPERATION_INFO, Action.GET_COURSES_BY_SCHOOL_YEAR);
                crit.addCriteria(Parameter.YEAR, ((SelectItem) event.getSource()).getValue().toString());
                reportGrid.fetchData(crit);
                reportGrid.deselectAllRecords();
            }
        });
        yearSelection.addChangedHandler(new ChangedHandler() {

            @Override
            public void onChanged(ChangedEvent event) {
                reportGrid.invalidateCache();
                Criteria crit = new Criteria(Parameter.OPERATION_INFO, Action.GET_COURSES_BY_SCHOOL_YEAR);
                crit.addCriteria(Parameter.YEAR, event.getValue().toString());
                reportGrid.fetchData(crit);
            }
        });

        DynamicForm form = new DynamicForm();
        form.setHeight(1);
        form.setWidth(75);
        form.setNumCols(1);
        form.setFields(yearSelection);

        SectionStackSection reportSection = new SectionStackSection(ECApplication.getConstants().course());
        reportSection.setCanCollapse(false);
        reportSection.setExpanded(true);
        reportSection.setItems(reportGrid);
        reportSection.setControls(totalItems, form);

        SectionStackSection detailSection = new SectionStackSection(ECApplication.getConstants().trainer());
        detailSection.setExpanded(true);
        detailSection.addItem(detailGrid);

        stack.setSections(reportSection, detailSection);
        mainLayout.setMembers(stack);

        addDrawHandler(new DrawHandler() {
            
            @Override
            public void onDraw(DrawEvent event) {
                yearSelection.fetchData();
            }
        });
        return mainLayout;
    }

    @Override
    protected String getHelpContent() {
        return "This is help content for manage course page";
    }

    /* (non-Javadoc)
     * @see com.binhthuan.catechist.client.AbstractContent#getPrintPreviewPanel()
     */
    @Override
    protected Canvas getPrintPreviewPanel() {
        return reportGrid;
    }
}
